{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "view-in-github"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/vitaldb/examples/blob/master/doublelow.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Note that <b>all users who use Vital DB, an open biosignal dataset, must agree to the Data Use Agreement below. \n",
    "</b> If you do not agree, please close this window. \n",
    "Click here: [Data Use Agreement](https://vitaldb.net/dataset/?query=overview&documentId=13qqajnNZzkN7NZ9aXnaQ-47NWy7kx-a6gbrcEsi-gak&sectionId=h.vcpgs1yemdb5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 539
    },
    "colab_type": "code",
    "id": "DuMrDqNxrkoY",
    "outputId": "d6b97f3c-a43b-4dd0-a668-898fde7f34bf"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       mbp_under_65  bis_under_20  double_low\n",
      "death                                        \n",
      "False     14.180582      0.393411    0.104326\n",
      "True      22.619255      2.094643    0.421868\n",
      "\n",
      "Group Comparison (mbp_under_65)\n",
      "T-test p-value=0.008757785748463904\n",
      "\n",
      "Univariate Logistic Regression (mbp_under_65)\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.490814\n",
      "         Iterations 5\n",
      "mbp_under_65\tb=0.015, exp(b)=1.015, pval=0.006\n",
      "\n",
      "Group Comparison (bis_under_20)\n",
      "T-test p-value=0.22635645589599399\n",
      "\n",
      "Univariate Logistic Regression (bis_under_20)\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.494882\n",
      "         Iterations 6\n",
      "bis_under_20\tb=0.064, exp(b)=1.066, pval=0.193\n",
      "\n",
      "Group Comparison (double_low)\n",
      "T-test p-value=0.07362792567203962\n",
      "\n",
      "Univariate Logistic Regression (double_low)\n",
      "       mbp_under_65  bis_under_20  double_low\n",
      "death                                        \n",
      "False     14.180582      0.393411    0.104326\n",
      "True      22.619255      2.094643    0.421868\n",
      "\n",
      "Group Comparison (mbp_under_65)\n",
      "T-test p-value=0.008757785748463904\n",
      "\n",
      "Univariate Logistic Regression (mbp_under_65)\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.490814\n",
      "         Iterations 5\n",
      "mbp_under_65\tb=0.015, exp(b)=1.015, pval=0.006\n",
      "\n",
      "Group Comparison (bis_under_20)\n",
      "T-test p-value=0.22635645589599399\n",
      "\n",
      "Univariate Logistic Regression (bis_under_20)\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.494882\n",
      "         Iterations 6\n",
      "bis_under_20\tb=0.064, exp(b)=1.066, pval=0.193\n",
      "\n",
      "Group Comparison (double_low)\n",
      "T-test p-value=0.07362792567203962\n",
      "\n",
      "Univariate Logistic Regression (double_low)\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.497650\n",
      "         Iterations 5\n",
      "double_low\tb=0.132, exp(b)=1.142, pval=0.123\n",
      "\n",
      "Optimization terminated successfully.\n",
      "         Current function value: 0.497650\n",
      "         Iterations 5\n",
      "double_low\tb=0.132, exp(b)=1.142, pval=0.123\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import pandas as df\n",
    "import numpy as np\n",
    "import scipy.stats as stat\n",
    "import statsmodels.api as sm\n",
    "\n",
    "# mbp<65, bis<20, mortality\n",
    "df = df.read_csv('https://raw.githubusercontent.com/vitaldb/examples/master/doublelow.csv')\n",
    "df['death'] = df['death'].astype(bool)\n",
    "\n",
    "# Summary\n",
    "print(df.groupby('death').mean())\n",
    "print()\n",
    "\n",
    "# For each variable\n",
    "for c in ['mbp_under_65', 'bis_under_20', 'double_low']:\n",
    "    a = df[df['death']][c]\n",
    "    b = df[~df['death']][c]\n",
    "\n",
    "    # Goup comparison\n",
    "    print('Group Comparison ({})'.format(c))\n",
    "    equal_var = stat.levene(a, b)[1] > 0.05  # levene의 등분산 검정\n",
    "    pval = stat.ttest_ind(a, b, equal_var=equal_var)[1]\n",
    "    print('T-test p-value={}'.format(pval))\n",
    "    print()\n",
    "\n",
    "    # Univariate logistic regression\n",
    "    print('Univariate Logistic Regression ({})'.format(c))\n",
    "    df['intercept'] = 1\n",
    "    model = sm.Logit(df['death'], df[['intercept', c]])\n",
    "    res = model.fit()\n",
    "    b = res.params[c]\n",
    "    pval = res.pvalues[c]\n",
    "    print('{}\\tb={:.3f}, exp(b)={:.3f}, pval={:.3f}'.format(c, b, np.exp(b), pval))\n",
    "    print()\n"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyNY9ITOFoqFcGvg/e5CbZlR",
   "collapsed_sections": [],
   "include_colab_link": true,
   "name": "doublelow.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
